#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NN=404;
vector<int>g[NN];
int p[NN];
ll a[NN],b[NN];
int main()
{
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<n;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)
		p[i]=i;
	ll ans=-1e18;
	do
	{
		memcpy(b,a,sizeof(b));
		ll res=0;
		for(int i=1;i<=n;i++)
		{
			res+=b[p[i]];
			for(int j=0;j<g[p[i]].size();j++)
				b[g[p[i]][j]]+=b[p[i]];
		}
		ans=max(ans,res);
	}while(next_permutation(p+1,p+1+n));
	printf("%lld",ans);
	return 0;
}
